#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper. 
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1




CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# Name of the source package
psource:=toshlcd-source

# The short upstream name, used for the module source directory
sname:=toshlcd

# prefix of the target package name
PACKAGE=toshlcd-modules

# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass

# load generic variable handling
-include $(MA_DIR)/include/generic.make

# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make

kdist_config: prep-deb-files


kdist_clean: clean
	[ ! -f Makefile ] || $(MAKE) KSRC=$(KSRC) clean
	dh_clean
configure: configure-stamp
configure-stamp:
	dh_testdir

	touch configure-stamp


build-arch: configure-stamp  build-arch-stamp
build-arch-stamp:
	dh_testdir

	touch $@

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules:
	dh_testroot
	dh_clean -k

	# Build the module
	$(MAKE) KSRC=$(KSRC) KVERS=$(KVERS)

	# Install the module
	$(MAKE) install KSRC=$(KSRC) DESTDIR=$(CURDIR)/debian/$(PKGNAME) DEPMOD="/bin/true"

	# Install the script
	$(MAKE) install-script BINDIR="/usr/bin"

	dh_installdocs
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)
	dh_clean -k

build-indep:  configure-stamp build-indep-stamp
build-indep-stamp:
	dh_testdir

	# Add here command to compile/build the arch indep package.
	# It's ok not to do anything here, if you don't need to build
	#  anything for this package.
	#docbook-to-man debian/toshlcd.sgml > toshlcd.1

	touch $@

build: build-arch build-indep

clean:
	dh_testdir
	#dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp

	# Add here commands to clean up after the build process.
#	-$(MAKE) clean

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(sname)/debian


	# Copy source
	cp Makefile debian/$(psource)/usr/src/modules/$(sname)/Makefile
	cp toshlcd.c debian/$(psource)/usr/src/modules/$(sname)/toshlcd.c
	cp AUTHORS debian/$(psource)/usr/src/modules/$(sname)/AUTHORS
	cp ChangeLog debian/$(psource)/usr/src/modules/$(sname)/ChangeLog
	cp COPYING debian/$(psource)/usr/src/modules/$(sname)/COPYING
	cp INSTALL debian/$(psource)/usr/src/modules/$(sname)/INSTALL
	cp README debian/$(psource)/usr/src/modules/$(sname)/README
	cp toshlcd debian/$(psource)/usr/src/modules/$(sname)/toshlcd


	# Copy debian directory
	cp debian/control.modules.in debian/$(psource)/usr/src/modules/$(sname)/debian/control.modules.in
	cp debian/rules debian/$(psource)/usr/src/modules/$(sname)/debian/rules
	cp debian/changelog debian/$(psource)/usr/src/modules/$(sname)/debian/changelog
	cp debian/copyright debian/$(psource)/usr/src/modules/$(sname)/debian/copyright
	cp debian/control debian/$(psource)/usr/src/modules/$(sname)/debian/control
	cp debian/compat debian/$(psource)/usr/src/modules/$(sname)/debian/compat
	cp debian/toshlcd-modules.postinst debian/$(psource)/usr/src/modules/$(sname)/debian/postinst
	cp debian/toshlcd-modules.postrm debian/$(psource)/usr/src/modules/$(sname)/debian/postrm
	cp debian/toshlcd-modules.prerm debian/$(psource)/usr/src/modules/$(sname)/debian/prerm


	# Create tar.bz2
	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules

	dh_install

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs  -i
	dh_installdocs -i
	dh_installexamples -i
#	dh_install -i
#	dh_installmenu -i
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_perl -i
#	dh_python -i
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -s
	dh_testroot -s
#	dh_installdebconf -s
	dh_installdocs -s
	dh_installexamples -s
	dh_installmenu -s
#	dh_installlogrotate -s
#	dh_installemacsen -s
#	dh_installpam -s
#	dh_installmime -s
#	dh_installinit -s
	dh_installcron -s
#	dh_installman -s
	dh_installinfo -s
	dh_installchangelogs  -s
	dh_strip -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
#	dh_makeshlibs -s
	dh_installdeb -s
#	dh_perl -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure binary-modules kdist kdist_configure kdist_image kdist_clean
